<template>
    <layout title="温馨提示" :showTabBar="false">
        <msg :title="title" :description="message" icon="info"></msg>
    </layout>
</template>

<script>
    import { Group, Datetime, XInput, FormPreview, XButton, Msg } from 'vux'
    import Layout from './Layout.vue'
    import { user } from '../api'
    import { cookie } from 'vux'

    export default {
        name: 'me',
        components: {
            Group,
            Datetime,
            XInput,
            FormPreview,
            XButton,
            Layout,
            Msg
        },
        data() {
            return {
                title: '自动登录中',
                message: '登录成功后会自动跳转到主页，请稍后',
            }
        },
        methods: {
            querystring(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            },
            async login(userid, code) {
                let ret = await this.$store.dispatch('login', {
                    code,
                    userid,
                })

                if (!ret.success) {
                    this.title = '登录失败'
                    this.message = ret.message || '请与管理员联系！'
                    return
                }
                this.$router.push({ path: '/' })
            },
        },
        async mounted() {
            this.$store.commit('showBack', false)
            let that = this

            //登录逻辑，检测是否包含userid参数，包含则认为是调试模式
            let userid = this.$route.query.userid
            if (userid) {
                this.message = '调试模式 切换用户Id'
                this.$store.dispatch('debug', {
                    debug: true
                })
                this.login(userid)
            } else {
                //记录调试模式
                let debug = this.querystring("debug")
                if (debug != null) {
                    this.message = '切换到调试模式'
                    this.$store.dispatch('debug', {
                        debug: true
                    })
                }
                //检测是否是钉钉客户端
                if (!dd.isDingtalk()) {
                    this.title = '请在钉钉客户端中打开网页'
                    this.message = ''
                }
                this.message = '配置授权中'
                dd.config(window.__config)
                this.message = '请求授权码'
                //交换code
                dd.ready(function () {

                    dd.runtime.permission.requestAuthCode({
                        corpId: "ding3a8c9da236345ba135c2f4657eb6378f",
                        onSuccess: function (result) {
                            const { code } = result
                            that.login(null, code)
                        },
                        onFail: function (error) {
                            that.message = error.message
                        }
                    })
                });
                dd.error(function (error) {
                    that.message = error.message
                });
            }
        }
    }

</script>
